package com.gxa.hualian.service.impl;

import com.gxa.hualian.exception.BusinessException;
import com.gxa.hualian.mapper.ManagerMapper;
import com.gxa.hualian.pojo.Manager;
import com.gxa.hualian.service.ManagerService;
import com.gxa.hualian.util.JsonResult;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2020/10/22 19:14
 * @Version 1.0
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {

    @Resource
    ManagerMapper managerMapper;

    @Override
    public Map add(Map<String,String> map) {
        Manager manager = new Manager();
     
        //根据电话查找管理员是否已存在
        manager.setPhone(map.get("phone"));
        Manager manager1 = managerMapper.selectOne(manager);
        JsonResult jsonResult = new JsonResult();
        if (manager1 == null){
            //如果不存在，继续执行新增
            manager.setName(map.get("name"));
            manager.setPhone(map.get("phone"));
            manager.setRegDate(new Date());
            manager.setState(0);
            managerMapper.insert(manager);

            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("400");
            jsonResult.setMsg("该电话已被注册");
        }
        return jsonResult.getValues();
    }

    @Override
    public Map login(String phone) {
        Manager manager = new Manager();
        JsonResult jsonResult = new JsonResult();
        if (phone.equals("")) {
            throw new BusinessException("电话不能为空");
        }
        manager.setPhone(phone);
        Manager manager1 = managerMapper.selectOne(manager);
        if (manager1==null){
            throw  new BusinessException("该电话未注册");
        }
        if(manager1.getState() == 0 ){
            throw new BusinessException("账号未启用，请联系超级管理员");
        }else {
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
        }
        return jsonResult.getValues();
    }

    @Override
    public int updateState(String phone,Integer state) {
        Manager manager = new Manager();
        JsonResult jsonResult = new JsonResult();
        manager.setPhone(phone);
        Manager manager1 = managerMapper.selectOne(manager);
        manager1.setState(state);
        int i = managerMapper.updateByPrimaryKeySelective(manager1);
        return i;
    }

    @Override
    public Map delete(String phone) {
        Manager manager = new Manager();
        JsonResult jsonResult = new JsonResult();
        manager.setPhone(phone);
        Manager manager1 = managerMapper.selectOne(manager);
        int i = managerMapper.deleteByPrimaryKey(manager1.getId());
        if (i == 1){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("400");
            jsonResult.setMsg("发生未知错误");
        }
        return jsonResult.getValues();
    }

    @Override
    public List<Manager> find(String field) {
        Manager manager = new Manager();
        JsonResult jsonResult = new JsonResult();
        manager.setName(field);
        manager.setPhone(field);
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orLike("name","%"+manager.getName()+"%");
        criteria.orLike("phone","%"+manager.getPhone()+"%");
        List<Manager> managers = managerMapper.selectByExample(example);
        return managers;
    }
}
